/**
 * 
 */
package com.googlecode.bestpaper.output.pdf.table;

import java.awt.Color;
import java.util.List;

import org.apache.commons.lang.ArrayUtils;

import com.googlecode.bestpaper.output.pdf.book.PDFParagraph;
import com.googlecode.bestpaper.syntax.BPMarkup;
import com.googlecode.bestpaper.syntax.table.BPTable;
import com.lowagie.text.Section;
import com.lowagie.text.Table;

/**
 * @author xalinx at gmail dot com
 * @date Dec 4, 2009
 */
public class PDFTable extends BPTable<Section, Table> {

	/**
	 * @param rowNum
	 * @param columnNum
	 */
	public PDFTable(int rowNum, int columnNum) {
		super(rowNum, columnNum);
	}

	public PDFTable(int columnNum) {
		super(columnNum);
	}

	public PDFTable() {
	}

	@Override
	protected Table buildSelf(Section writer, int fontStyle) throws Exception {
		Table t = new Table(getColumnNum(), getRowNum());
		t.setBorderColor(new Color(0, 0, 0));
		t.setBorderWidth(1);
		t.setPadding(3);
		t.setSpacing(0);
		writer.add(t);
		return t;
	}

	@Override
	public <E extends BPMarkup> void addChild(E child) {
		BPMarkup tmp = child;
		if (tmp instanceof PDFParagraph) {
			PDFCell cell = new PDFCell();
			PDFParagraph tmpPara = (PDFParagraph) tmp;
			List<BPMarkup> children = tmpPara.getChildren();
			if(children != null) {
				for(BPMarkup mark: children) {
					cell.addChild(mark);
				}
			}
			tmp = cell;
		}
		super.addChild(tmp);
	}

}
